<?php

namespace co2_OS;

class sysvar

{
	protected static
		$sysvars
		;

	public function __construct()
	
	{
		if(is_null(self::$sysvars=(object)parse_ini_file(sysvar) ))
		throw new \Exception('co2: fatal boot error: could not read sysvar');
		
		foreach(self::$sysvars as $k=>$v)
		if(preg_match('/%\{.+\}/',$v))
		self::$sysvars->$k	=	preg_replace('/%\{(.+)\}/Ue','print_r(self::$sysvars->{"$1"},1);',$v);
	}
	
	public static function
	get(
		$key=null
	)
	
	{
		if(is_null($key))
		return self::$sysvars;
		
		if(isset(self::$sysvars->$key))
		return self::$sysvars->$key;
	
		return $key;
	}
	
	public static function
	edit(
		$key,
		$value=false
	)
	
	{
		if(func_num_args()==1) // delete
		unset(self::$sysvars);
		else
		self::$sysvars->$key=$value;
		
		self::save();
	}
	
	public static function
	save()
	
	{
		$string=array();
		
		foreach(self::$sysvars as $k =>$v)
		$string[]="$k=$v";
	
		\co2\call('\co2\File',sysvar)->put(implode(PHP_EOL,$string));
	}
}
